package org.origin.ferret.system.mapstruct;

import org.apache.commons.lang.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.origin.centre.support.utils.BasicUtil;
import org.origin.ferret.system.model.dto.client.SysClientInsertDto;
import org.origin.ferret.system.model.dto.client.SysClientUpdateDto;
import org.origin.ferret.system.model.entity.SysClientEntity;
import org.origin.ferret.system.model.vo.client.SysClientVo;

import java.math.BigDecimal;

/**
 * 系统渠道对象-转换
 *
 * @author ferret
 * @version 2024-05-08
 */
@Mapper(componentModel = "spring")
public interface SysClientMapstruct {

    SysClientEntity toEntity(SysClientInsertDto insertDto);

    SysClientEntity toEntity(SysClientUpdateDto updateDto);

    @Mapping(target = "liveTime", expression = "java(getLiveTime(entity.getTokenSettings()))")
    SysClientVo toVo(SysClientEntity entity);

    default Integer getLiveTime(String tokenSettings) {
        if (StringUtils.isNotBlank(tokenSettings)) {
            String liveTimeStr = BasicUtil.getStartOfEnd(tokenSettings, "\"settings.token.device-code-time-to-live\":[\"java.time.Duration\",", "]}");
            if (StringUtils.isNotBlank(liveTimeStr)) {
                return new BigDecimal(liveTimeStr).intValue();
            }
        }
        return 3600;
    }
}
